﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sausage.Web.DataModel;

namespace Sausage.Web.Domain
{
    public class MenuInfo
    {
        public MenuCategory Category;
        public Menu Menu;
    }

    public class MenuHelper
    {
        public static List<MenuInfo> GetMenuListByRole(List<Role> targetRoleList)
        {
            List<Guid> list = targetRoleList.Select(item => item.RoleID).ToList();
            using (var context = new SausageEntities())
            {
                var data = (from menu in context.Menu
                            join menuCategory in context.CategoryMenu on menu.MenuID equals menuCategory.MenuID
                            join category in context.MenuCategory on menuCategory.CategoryID equals category.CategoryID
                            join menuRole in context.MenuRole on menu.MenuID equals menuRole.MenuID
                            join role in context.Role on menuRole.RoleID equals role.RoleID
                            where list.Contains(role.RoleID)
                            orderby category.TheOrder, menuCategory.TheOrder
                            select new MenuInfo
                                       {
                                           Menu = menu,
                                           Category = category,
                                       }
                       ).ToList();

                var resultList = new List<Guid>();
                var result = new List<MenuInfo>();

                foreach (var menuInfo in data)
                {
                    if (!resultList.Contains(menuInfo.Menu.MenuID))
                    {
                        resultList.Add(menuInfo.Menu.MenuID);
                        result.Add(menuInfo);
                    }
                }

                return result;
            }
        }

        /// <summary>
        /// 渲染菜单模型。
        /// </summary>
        /// <returns>渲染出来的菜单模型。</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2012/03/08 17:03    创建
        /// </history>
        public static List<string> BuildRenderModel()
        {
            //获得所有的菜单列表
            List<MenuInfo> menus = Helper.CurrentPrincipal.MeanList;

            //用于保存菜单模型信息
            List<string> menuModels = new List<string>();

            //用于保存菜单的脚本
            StringBuilder builder = new StringBuilder();

            //用于记录是否为第一行
            bool isFirstRow = true;

            //用于保存标题
            string title = string.Empty;

            //用于保存分组标题
            string groupTitle = string.Empty;

            //记录菜单的分类
            MenuCategory category = null;

            //判断是否有菜单
            if (menus != null && menus.Count > 0)
            {
                //记录分类
                category = menus[0].Category;

                //循环添加
                foreach (MenuInfo menu in menus)
                {
                    //判断是否为第一行
                    if (isFirstRow)
                    {
                        //添加开始标记
                        builder.Append("<ul class=\"Menu\">");
                    }

                    //判断是否为最后一行
                    if (category.CategoryID != menu.Category.CategoryID)
                    {
                        //添加结束标记
                        builder.Append("</ul>");

                        //生成标题
                        title = string.Format("<span class=\"{0}\">{1}</span>", category.Style ?? "MenuTitle", category.CategoryName);

                        //生成分组标题
                        groupTitle = string.Format("<span class=\"{0}\">{1}</span>", category.GroupStyle ?? "MenuGroupTitle", category.GroupName);

                        //添加到列表
                        menuModels.Add(string.Format(@"{{ Title: '{0}', Html: '{1}', ID:'{2:B}', Group:'{3}', GroupTitle:'{4}'}}", title, builder.ToString(), category.CategoryID, category.GroupName, groupTitle));

                        //清除内容
                        builder.Clear();

                        //添加开始标记
                        builder.Append("<ul class=\"Menu\">");
                    }

                    //判定是否设置了菜单样式
                    if (!string.IsNullOrEmpty(menu.Menu.Style))
                    {
                        //添加菜单
                        builder.AppendFormat("<li><a class=\"{0}\" href=\"{1}\">{2}</a></li>", menu.Menu.Style, menu.Menu.SourceUrl, menu.Menu.MenuName);
                    }
                    else
                    {
                        //添加菜单
                        builder.AppendFormat("<li><a href=\"{0}\">{1}</a></li>", menu.Menu.SourceUrl, menu.Menu.MenuName);
                    }

                    //记录分类编号
                    category = menu.Category;
                }

                //添加结束标记
                builder.Append("</ul>");

                //生成标题
                title = string.Format("<span class=\"MenuTitle\">{0}</span>", category.CategoryName);

                //生成分组标题
                groupTitle = string.Format("<span class=\"MenuGroupTitle\">{0}</span>", category.GroupName);

                //添加到列表
                menuModels.Add(string.Format(@"{{ Title: '{0}', Html: '{1}', ID:'{2:B}', Group:'{3}', GroupTitle:'{4}'}}", title, builder.ToString(), category.CategoryID, category.GroupName, groupTitle));

                //清除内容
                builder.Clear();
            }

            //返回模型
            return menuModels;
        }
    }
}
